package mipushdemo.xiaomi.com.diyitonghang.ui.fragment.home;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import mipushdemo.xiaomi.com.diyitonghang.R;
import mipushdemo.xiaomi.com.diyitonghang.api.ApiService;
import mipushdemo.xiaomi.com.diyitonghang.base.BaseActivity;
import mipushdemo.xiaomi.com.diyitonghang.data.bean.BaseHotSeacrch;
import mipushdemo.xiaomi.com.diyitonghang.data.bean.MessageE;
import mipushdemo.xiaomi.com.diyitonghang.ui.adapter.AdapterHotSearcher;
import mipushdemo.xiaomi.com.diyitonghang.ui.adapter.ChannelViewPage;
import mipushdemo.xiaomi.com.diyitonghang.ui.fragment.HuatiFragment;
import mipushdemo.xiaomi.com.diyitonghang.utils.RetrofitUtils;
import okhttp3.ResponseBody;

public class SearchDataActivity extends BaseActivity {
    @BindView(R.id.classification_search)
    EditText classificationSearch;
    @BindView(R.id.classification_clean)
    TextView classificationClean;
    @BindView(R.id.classification_cancel)
    TextView classificationCancel;
    @BindView(R.id.sousuolishi)
    TextView sousuolishi;
    @BindView(R.id.qingkonglishi)
    TextView qingkonglishi;
    @BindView(R.id.wurenjia)
    TextView wurenjia;
    @BindView(R.id.remensousuo)
    TextView remensousuo;
    @BindView(R.id.ryReMenSouSuo)
    RecyclerView ryReMenSouSuo;
    @BindView(R.id.autolishi)
    RelativeLayout autolishi;
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.searchHuaTiAndWenZhang)
    ViewPager searchHuaTiAndWenZhang;
    @BindView(R.id.autowenzhangAndhuati)
    LinearLayout autowenzhangAndhuati;
    private ArrayList<BaseHotSeacrch> list = new ArrayList<>();
    private List<String> listName = new ArrayList<>();
    private ArrayList<String>  listTab = new ArrayList<>()
    ;
    private ArrayList<Fragment> listFragment = new ArrayList<>();
    private String s;
    public static  String title;
    private ChannelViewPage channelViewPage;

    @Override
    protected int windowColor() {
        return R.color.colorRed;
    }

    @Override
    protected void init() {
        changeStatusBarTextColor(true);

        requestData();

        listener();


    }

    private void listener() {
        //请空按钮的监听
        classificationClean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                classificationSearch.setText("");
            }
        });
        //搜索框的监听
        classificationSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                s = classificationSearch.getText().toString();
                // Log.e("sssssss",s+"kong");
                if (s.length() != 0) {
                    classificationCancel.setText("搜索");
                } else {
                    classificationCancel.setText("取消");

                }
            }
        });


        //搜索按钮的监听
        classificationCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String s1 = classificationCancel.getText().toString();
                if ("搜索".equals(s1)) {
                    autolishi.setVisibility(View.GONE);
                    autowenzhangAndhuati.setVisibility(View.VISIBLE);
                    title = classificationSearch.getText().toString();
                    dataSearch(title);
                } else if("取消".equals(s1)){
                    autolishi.setVisibility(View.VISIBLE);
                    autowenzhangAndhuati.setVisibility(View.GONE);
                    classificationSearch.setText("");
                }
            }
        });
        //展示搜索后的数据
    }

    private void dataSearch(String title) {

        listTab.clear();
        listFragment.clear();
            listTab.add("文章");
            listTab.add("话题");
                SearchFragment fragment = SearchFragment.getFragment(listTab.get(0), title);
        HuatiFragment huatiFragment = HuatiFragment.newInstance(listTab.get(1), title);
        listFragment.add(fragment);
        listFragment.add(huatiFragment);
        channelViewPage = new ChannelViewPage(getSupportFragmentManager(), listTab, listFragment);
        searchHuaTiAndWenZhang.setAdapter(channelViewPage);
        tablayout.setTabTextColors(Color.parseColor("#222222"),Color.parseColor("#d53d3c"));
        tablayout.setSelectedTabIndicatorColor(Color.parseColor("#d53d3c"));
        tablayout.setupWithViewPager(searchHuaTiAndWenZhang);
    }

    private void requestData() {
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        ApiService apiService = RetrofitUtils.newInstance().netWorkRequest();
        Observable<ResponseBody> observable = apiService.homeSearchData("application/x-www-form-urlencoded");
        observable.observeOn(Schedulers.newThread())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            String string = responseBody.string();
                            JSONObject jsonObject = new JSONObject(string);
                            JSONObject data = jsonObject.getJSONObject("data");
                            JSONArray searchList = data.getJSONArray("searchList");
                            for (int i = 0; i < searchList.length(); i++) {
                                JSONObject jsonObject1 = searchList.getJSONObject(i);
                                String content = jsonObject1.optString("content");
                                String searchId = jsonObject1.optString("searchId");
                                list.add(new BaseHotSeacrch(content, searchId));
                                if (i == 0) {
                                    listName.add(1 + "." + content);
                                } else if (i == 1) {
                                    listName.add(2 + "." + content);
                                } else if (i == 2) {
                                    listName.add(3 + "." + content);
                                } else {
                                    listName.add(i + 1 + "." + content);
                                }

                            }
                            // Log.e("listname", listName.size() + "s");
                            GridLayoutManager gridLayoutManager = new GridLayoutManager(SearchDataActivity.this, 1);
                            ryReMenSouSuo.setLayoutManager(gridLayoutManager);
                            AdapterHotSearcher adapterHotSearcher = new AdapterHotSearcher(R.layout.layout_adapterstring, listName);
                            ryReMenSouSuo.setAdapter(adapterHotSearcher);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void loadData() {

    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_data;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
